<?php

namespace common\models\demand;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%demand_attach}}".
 *
 * @property int $id
 * @property int $user_id
 * @property int $demand_id
 * @property int $solution_id
 * @property string|null $format
 * @property string|null $path
 * @property string|null $ext
 * @property string|null $description
 * @property int $created_at
 * @property int $updated_at
 */
class DemandAttach extends \common\db\ActiveRecord {

    use \common\traits\relation\User;

    const FORMAT_IMAGE = 'image';
    const FORMAT_FILE = 'file';
    const FORMAT_VIDEO = 'video';
    const FORMAT_MEDIA = 'media';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%demand_attach}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'demand_id', 'solution_id', 'created_at', 'updated_at'], 'integer'],
            [['format'], 'string', 'max' => 50],
            [['ext'], 'string', 'max' => 50],
            [['path', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'demand_id' => 'Article ID',
            'solution_id' => 'Attach ID',
            'path' => 'Path',
            'description' => 'Description',
            'format' => 'format',
            'ext' => 'ext',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getPathUrl() {
        return Url::attach($this->path);
    }

    public function getFilename() {
        return $this->description . '.' . $this->ext;
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'path' => 'pathUrl',
            'format',
            'filename',
        ];
    }

}
